<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *消费排行
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'listd';
$tabs=array(
    array('op'=>'listd','title'=>'日消费排行'),
    array('op'=>'listm','title'=>'月消费排行'),
    array('op'=>'listy','title'=>'年消费排行'),
    array('op'=>'listz','title'=>'总消费排行（计算量大会卡顿）'),
);
$y=$_RQ['y']?:date('Y');
$m=$_RQ['m']?:date('m');
$d=$_RQ['d']?:date('d');
$return['params']['y']=$y;
$return['params']['m']=intval($m).'';
$return['params']['d']=intval($d).'';
if($_RQ['nickname']){
$return['params']['nickname']=$_RQ['nickname'];
}
if($_RQ['level']){
$return['params']['level']=$_RQ['level'];
}
$return['levels']=$this->getall('member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
//添加默认等级
$return['levels']['-1']=array(
		'id'=>'0',
		'title'=>'默认会员',
		'level'=>0,
		'discount'=>'0',
		'ordermoney'=>'0.00',
		'ordercount'=>'0',
		'status'=>'1',
);
if($_RQ['op']=='listd'){
    $pmrq=$y.'.'.$m.'.'.$d;
    $d2=$d+1;
    $starttime=strtotime($y.'-'.$m.'-'.$d);
    $endtime=strtotime($y.'-'.$m.'-'.$d2);
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
        array('name'=>'m','width'=>15,'title'=>'月','type'=>'radio','options'=>'months'),
        array('name'=>'d','width'=>15,'title'=>'日','type'=>'radio','options'=>'days'),
    );
}
if($_RQ['op']=='listm'){
    $pmrq=$y.'.'.$m;
    $m2=$m+1;
    $starttime=strtotime($y.'-'.$m.'-1');
    $endtime=strtotime($y.'-'.$m2.'-1');
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
        array('name'=>'m','width'=>15,'title'=>'月','type'=>'radio','options'=>'months'),
		array('name'=>'level','width'=>15,'title'=>'会员等级','type'=>'select','options'=>'levels'),
		array('name'=>'nickname','width'=>15,'title'=>'会员昵称','type'=>'string'),
    );
}
if($_RQ['op']=='listy'){
    $pmrq=$y;
    $y2=$y+1;
    $starttime=strtotime($y.'-1-1');
    $endtime=strtotime($y2.'-1-1');
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
    );
}

$params=' and createtime>'.$starttime.' and createtime<'.$endtime;

if($_RQ['op']=='listz'){
    $params='';
    $search='';
}

$return['list']=pdo_fetchall('SELECT mid,SUM(price),count(1) FROM '.tablename('jy_weishop_order').' WHERE ispay=1 and uniacid= '.$_SESSION['uniacid'].$params.' group by mid');
foreach($return['list'] as $k=>&$v){
    $member=pdo_get('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','nickname','mobile'));
	$member2=pdo_get('jy_weishop_member',array('mid'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
    $member3=pdo_get('jy_weishop_member_level',array('id'=>$member2['level'],'uniacid'=>$_SESSION['uniacid']),'title');
    if(!$member['id']){
        unset($return['list'][$k]);
        continue;
    }
    $v['nickid']=$v['mid'];
    $v['nickname']=$member['nickname'];
    $v['level']=$member3['title'];
    $v['mobile']=$member['mobile'];
    $v['total']=$v['count(1)'];
    $v['totalprice']=$v['SUM(price)'];
}
paixu($return['list'],'SUM(price)','desc');
$i=0;
foreach($return['list'] as $k=>&$v){
    $v['rank']=$i+1;
	$i++;
	if($_RQ['nickname'] && strpos($v['nickname'],$_RQ['nickname']) === false){
		unset($return['list'][$k]);
		continue;
	}
	if($_RQ['level'] && ($v['level']!=$_RQ['level'] || (!$v['level'] && $_RQ['level']>0))){
		unset($return['list'][$k]);
		continue;
	}
}
$htmls=array(
    'type'=>'list',
    'tabs'=>$tabs,
    'search'=>$search,
    'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
        array('name'=>'rank','title'=>$pmrq.' 排名','type'=>'string'),
        array('name'=>'nickid','title'=>'会员ID','type'=>'string'),
        array('name'=>'nickname','title'=>'会员昵称','type'=>'string'),
        array('name'=>'level','title'=>'商城等级','type'=>'string'),
        array('name'=>'mobile','title'=>'手机','type'=>'string'),         
        array('name'=>'totalprice','title'=>'消费金额','type'=>'string'),
        array('name'=>'total','title'=>'订单数','type'=>'string'),
    )),
);
$return['htmls']=$htmls;
$nowy=date('Y');
$y1=$nowy-2;
$y2=$nowy-1;
$return['ys']=array(
    $y1=>$y1.'年',
    $y2=>$y2.'年',
    $nowy=>$nowy.'年' ,
);
for($i=1;$i<=31;$i++){
    $days[$i]=$i.'日';
    if($i<=12){$months[$i]=$i.'月';}
}
$return['months']=$months;
$return['days']=$days;
exi($return);